var im = 0;
toast("音量上停止；音量下暂停、继续");


threads.start(function() {
    while (true) {
        if (im == 1) {
            lsx();
            toastLog("弹奏结束")
            exit();
        } else {
            sleep(1000);
        }
        sleep(1000);
    }
});
//新线程end
events.observeKey();
events.setKeyInterceptionEnabled("volume_down",true);
events.setKeyInterceptionEnabled("volume_up",true);

events.onKeyDown("volume_up", function(event) {
    toastLog("结束歌曲");
    exit();
});
events.onKeyDown("volume_down", () => {
    log("音量下键被按下");
    if (im == 1) {
        im = 0;
        toast("暂停");
    } else {
        im = 1;
        toast("开始，继续");
    }

});
//toast("666");
sleep(1000);

function lsx() {

m = Music.tracks;
 var sz=[]
 for(c=0;c<m.length;c++){
    var sz=sz.concat(m[c].notes)
 }
 var d = sz.sort(function(a, b) {
     return a.time - b.time;
 });
 function autoWireResult(keyNumListTemp, n, t, result) {
     if (keyNumListTemp != null && keyNumListTemp.length > 0) {
         result.push([
             keyNumListTemp,
             n,
             t
         ]);
     } else {
         var temp = result[result.length - 1];
     }
 }
  data = d;
 var lastTime = 0;
 var result = [];
 var keyNumListTemp = [];
 var key按住时间 = [];
 for (var i = 0; i < data.length; i++) {
     now = data[i];
     nowTime = now.time;
     sleeptime = Number(nowTime) - Number(lastTime);
     if (sleeptime >= 0.01) {
         autoWireResult(keyNumListTemp, key按住时间, sleeptime, result);
         keyNumListTemp = [];
         key按住时间 = [];
     }
     nowPitches = now.midi;
     now按住时间 = now.duration;
     lastTime = nowTime;
     keyNumListTemp.push(Number(nowPitches));
     key按住时间.push(Number(now按住时间));
 }
var bai = {
        21: [118, 994],
            23: [168, 994],
                24: [207, 994],
                    26: [258, 994],
                        28: [302, 994],
                            29: [350, 994],
                                31: [377, 994],
                                    33: [437, 994],
                                        35: [488, 994],
                                            36: [527, 994],
                                                38: [567, 994],
                                                    40: [613, 994],
                                                        41: [653, 994],
                                                            43: [700, 994],
                                                                45: [740, 994],
                                                                    47: [790, 994],
                                                                        48: [830, 994],
                                                                            50: [878, 994],
                                                                                52: [921, 994],
                                                                                    53: [963, 994],
                                                                                        55: [1012, 994],
                                                                                            57: [1056, 994],
                                                                                                59: [1102, 994],
                                                                                                    60: [1144, 994],
                                                                                                        62: [1186, 994],
                                                                                                            64: [1231, 994],
                                                                                                                65: [1275, 994],
                                                                                                                    67: [1318, 994],
                                                                                                                        69: [1362, 994],
                                                                                                                            71: [1410, 994],
                                                                                                                                72: [1454, 994],
                                                                                                                                    74: [1497, 994],
                                                                                                                                        76: [1546, 994],
                                                                                                                                            77: [1588, 994],
                                                                                                                                                79: [1632, 994],
                                                                                                                                                    81: [1677, 994],
                                                                                                                                                        83: [1721, 994],
                                                                                                                                                            84: [1760, 994],
                                                                                                                                                                86: [1810, 994],
                                                                                                                                                                    88: [1856, 994],
                                                                                                                                                                        89: [1890, 994],
                                                                                                                                                                            91: [1940, 994],
                                                                                                                                                                                93: [1987, 994],
                                                                                                                                                                                    95: [2036, 994],
                                                                                                                                                                                        96: [2070, 994],
                                                                                                                                                                                            98: [2121, 994],
                                                                                                                                                                                                100: [2166, 994],
                                                                                                                                                                                                    101: [2203, 994],
                                                                                                                                                                                                        103: [2250, 994],
                                                                                                                                                                                                            105: [2296, 994],
                                                                                                                                                                                                                107: [2344, 994],
                                                                                                                                                                                                                    108: [2384, 994],

                                                                                                                                                                                                                        //黑键
                                                                                                                                                                                                                            22: [138, 823],
                                                                                                                                                                                                                                25: [235, 801],
                                                                                                                                                                                                                                    27: [267, 778],
                                                                                                                                                                                                                                        30: [365, 822],
                                                                                                                                                                                                                                            32: [405, 813],
                                                                                                                                                                                                                                                34: [461, 800],
                                                                                                                                                                                                                                                    37: [539, 812],
                                                                                                                                                                                                                                                        39: [591, 833],
                                                                                                                                                                                                                                                            42: [675, 828],
                                                                                                                                                                                                                                                                44: [736, 835],
                                                                                                                                                                                                                                                                    46: [760, 797],
                                                                                                                                                                                                                                                                        49: [855, 796],
                                                                                                                                                                                                                                                                            51: [900, 790],
                                                                                                                                                                                                                                                                                54: [992, 831],
                                                                                                                                                                                                                                                                                    56: [1014, 813],
                                                                                                                                                                                                                                                                                        58: [1071, 805],
                                                                                                                                                                                                                                                                                            61: [1174, 803],
                                                                                                                                                                                                                                                                                                63: [1207, 788],
                                                                                                                                                                                                                                                                                                    66: [1312, 804],
                                                                                                                                                                                                                                                                                                        68: [1358, 801],
                                                                                                                                                                                                                                                                                                            70: [1395, 801],
                                                                                                                                                                                                                                                                                                                73: [1474, 813],
                                                                                                                                                                                                                                                                                                                    75: [1526, 828],
                                                                                                                                                                                                                                                                                                                        78: [1604, 806],
                                                                                                                                                                                                                                                                                                                            80: [1661, 816],
                                                                                                                                                                                                                                                                                                                                82: [1696, 809],
                                                                                                                                                                                                                                                                                                                                    85: [1787, 806],
                                                                                                                                                                                                                                                                                                                                        87: [1833, 804],
                                                                                                                                                                                                                                                                                                                                            90: [1923, 804],
                                                                                                                                                                                                                                                                                                                                                92: [1967, 801],
                                                                                                                                                                                                                                                                                                                                                    94: [2009, 836],
                                                                                                                                                                                                                                                                                                                                                        97: [2102, 818],
                                                                                                                                                                                                                                                                                                                                                            99: [2138, 792],
                                                                                                                                                                                                                                                                                                                                                                102: [2233, 781],
                                                                                                                                                                                                                                                                                                                                                                    104: [2281, 799],
                                                                                                                                                                                                                                                                                                                                                                        106: [2323, 820],

};


let xy = [],speedControl = 1;
function ran(c) {
    c = c | 20;
    return Math.random() * c - c / 2;
}
function pre(item) {
        let items = [],
        keys = item[0];//按下的琴键
	if(item[2]-Math.max.apply(null,item[1])>0){
		var pressTime = item[1];
		var sleepTime = item[2]-Math.max.apply(null,item[1]);
	}else{
		var a = Math.max.apply(null,item[1]);
		var b = item[2];
		var c = b/a;
		for(i in item[1]){
			item[1][i] = item[1][i]*c;
		}
		var pressTime = item[1]
		var sleepTime = 0.001;
	}
    for (i in keys) {

        let id = keys[i],
        x=bai[id][0];
        y= bai[id][1];
      let press = pressTime[i];
      
        items.push([Math.round(press*1000), [x, y],
            [x, y]
        ]);
    }
    if (items.length > 0) {
        //log(items)
        gestures.apply(null, items);
    }
    sleep(Math.round(sleepTime*1000));
}
list=result;

for (i in list) {
    pre(list[i]);
    while (im != 1) {
        sleep(100);
    }
    
}
//toast("弹奏结束")

}